home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / rc-1.000 / rc-1 / rc-1.5-linux / b_getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  1.0 KB  |  46 lines

  1. #include "addon.h"
  2. typedef enum bool { FALSE, TRUE } bool;
  3. extern void set(bool);
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. extern int getopt __P((int __argc,char *__const * __argv,__const char *__opts));
  8. extern int opterr;
  9. extern int optind;
  10. extern int optopt;
  11. extern char *optarg;
  12.  
  13. void b_getopt(char** argv) {
  14.     int c, argc=0;
  15.     char* flags;
  16.  
  17. /*Count No. args*/
  18.     while (argv[argc]!=0)
  19.         argc++;
  20.  
  21. /*Check No. args*/
  22.     if (argc<2) {
  23.        fprintf(stderr, "usage: %s flag-specification arg-list\n", argv[0]);
  24.        set(FALSE);
  25.     }
  26.  
  27. /*Store location of the flags (arg 1), then shift*/
  28.     flags=argv[1];
  29.     argv[1]=argv[0];
  30.     argv++;
  31.     argc--;
  32.  
  33. /*Print flags (assumes multiple whitespace collapsed in cmd substitution)*/
  34.     while ((c=getopt(argc, argv, flags)) != EOF) {
  35.         if (c=='?') { set(FALSE); return; }
  36.         printf("-%c %s ", c, index(flags, c)[1] == ':' ? optarg : "");
  37.     }
  38.  
  39. /*Print rest of options*/
  40.     printf("-- ");
  41.     for (argv+=optind; *argv; argv++)
  42.         printf("%s ", *argv);
  43.     set(TRUE);
  44.     return;
  45. }
  46.